package org.weed.remote.http;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;
import java.util.HashMap;
import java.util.Map;

public class HttpResponse implements Runnable {
	private HttpConnection httpConnection;
	private ByteBuffer buffer;
	private Map<String, String> headers;
	private File file;
	
	public HttpResponse(HttpConnection httpConnection) {
		this.httpConnection = httpConnection;
		buffer = ByteBuffer.allocate(HttpConstants.BUFFER_SIZE);
		headers = new HashMap<String, String>();
	}
	public void setFile(File file) {
		this.file = file;
	}

	public void run() {
		try {
			SocketChannel socketChannel = httpConnection.getSocketChannel();
			System.out.println("Sending response to " + socketChannel.getRemoteAddress());
			//byte[] content = "<html><header></header><body>ttt</body></html>".getBytes();
			if (!file.exists()) {
				System.out.println("creating dir:"+file.getName());
				System.out.println(file.mkdir());
				byte[] content = "<html><header></header><body>Directory created!!</body></html>".getBytes();
				String header = "HTTP/1.1 200 OK\n" + 
						"Content-Type: text/html\n" + 
						"Content-Length: " + content.length + "\n" +
						"\n";
				socketChannel.write(ByteBuffer.wrap(header.getBytes()));
				socketChannel.write(ByteBuffer.wrap(content));
			} else if (file.isDirectory()) {
				StringBuilder builder = new StringBuilder();
				builder.append("<html><header></header><body>");
				builder.append("<table><tr><td>File Name</td><td>File Size</td></tr>");
				for (File subfile : file.listFiles()) {
					builder.append("<tr><td><a href=\"");
					builder.append(subfile.getName());
					builder.append("\">");
					builder.append(subfile.getName());
					builder.append("</a></td><td>");
					builder.append(subfile.getPath());
					builder.append("</td><tr>");
				}
				
				builder.append("</table></body></html>");
				byte[] content = builder.toString().getBytes();
				String header = "HTTP/1.1 200 OK\n" + 
						"Content-Type: text/html\n" + 
						"Content-Length: " + content.length + "\n" +
						"\n";
				socketChannel.write(ByteBuffer.wrap(header.getBytes()));
				socketChannel.write(ByteBuffer.wrap(content));
			} else {
				RandomAccessFile rafile = new RandomAccessFile(file, "r");
				
				FileChannel fChannel = rafile.getChannel();
				String header = "HTTP/1.1 200 OK\n" + 
						"Content-Type: application/octet-stream\n" + 
						"Content-Length: " + fChannel.size() + "\n" +
						"Content-disposition: attachment; filename=" + file.getName() + "\n" +
						"\n";
				socketChannel.write(ByteBuffer.wrap(header.getBytes()));
				int bytesRead;
				int bytesWrite;
				buffer.clear();
				while ((bytesRead = fChannel.read(buffer)) != -1) {
					buffer.flip();
					bytesWrite = socketChannel.write(buffer);
					buffer.compact();
					System.err.println("Read: " + bytesRead + " bytes"+", Write: " + bytesWrite + " bytes.");
				}
				fChannel.close();
			}
			httpConnection.registerEvent(HttpConstants.READ);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void reset() {
		headers.clear();
	}
	
	private void sendHttpResponseHeaders() {
		StringBuilder builder = new StringBuilder();
		
	}

}
